package yo.latyoo.hokage.zetsu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import yo.latyoo.hokage.tools.consts.LogConst;
import yo.latyoo.hokage.tools.util.StringUtil;
import yo.latyoo.hokage.zetsu.executor.ZetsuSpringExecutor;

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties({HokageProperties.class, ServerProperties.class})
public class ZetsuAutoConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(ZetsuAutoConfiguration.class);
    @Bean
    public ZetsuSpringExecutor jobExecutor(HokageProperties prop, ServerProperties sp) {
        logger.debug(LogConst.PREFIX_ZETSU + ",{}",prop);
        ZetsuSpringExecutor jobExecutor = new ZetsuSpringExecutor();
        jobExecutor.accessToken = prop.getAccessToken();
        jobExecutor.logRetentionDays = prop.getLogRetentionDays();
        jobExecutor.skmrAddress = prop.getSkmrAddress();
        //String ip = StringUtil.isNull(prop.getMyIp()) ? IpUtil.getServerIp(): prop.getMyIp().trim();
        //jobExecutor.myAddress = ip.concat(":").concat(String.valueOf(sp.getPort()));
        jobExecutor.logPath = prop.getLogPath();
        jobExecutor.appName = prop.getAppName();
        return jobExecutor;
    }




}

@ConfigurationProperties(prefix = "latyoo.hokage")
class HokageProperties{
    private String appName;
    private String skmrAddress;
    private String myIp;
    private String logPath;
    private Integer logRetentionDays;
    private String accessToken;

    @Override
    public String toString() {
        return "latyoo-hokage-job config -> {" +
                ", appName=" + appName +
                "skmrAddress='" + skmrAddress + '\'' +
                ", logPath='" + logPath + '\'' +
                ", logRetentionDays=" + logRetentionDays +
                '}';
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getSkmrAddress() {
        return skmrAddress;
    }

    public void setSkmrAddress(String skmrAddress) {
        this.skmrAddress = skmrAddress;
    }

    public String getMyIp() {
        return myIp;
    }

    public void setMyIp(String myIp) {
        this.myIp = myIp;
    }

    public String getLogPath() {
        return logPath;
    }

    public void setLogPath(String logPath) {
        this.logPath = logPath;
    }

    public Integer getLogRetentionDays() {
        return logRetentionDays;
    }

    public void setLogRetentionDays(Integer logRetentionDays) {
        this.logRetentionDays = logRetentionDays;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }
}
